package com.sunsys.QuestionAnswer;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ServletLogin
 */
//@WebServlet("/ServletLogin")

public class UserLogin extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html");
		PrintWriter writer = response.getWriter();
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		UserDAO user = new UserDAO();
		int userId = user.login(username, password);
		HttpSession session = request.getSession(true) ;
		session.setAttribute("userId",userId);
		if(userId!=0) {
			session.setAttribute("username", username);
			writer.println("<table>");
			writer.println("<tr >  <td> UserName:"+username+"</td></tr>"); 
			writer.println("<tr> <td> <a href=\"Search.html\"><b> SEARCH </b> </a> </td>");
			writer.println("<td> <a href=\"Tags.html\"><b> TAGS </b> </a> </td>");
			writer.println("<td> <a href=\"Questions.jsp\"><b> QUESTIONS </b> </a> </td>");
			writer.println("<td> <a href=\"QuestionPost.jsp\"><b> POST </b> </a> </td>");
			writer.println("</table>");
		}
		else {
			writer.println("Invalid Username/Password");
		}
		
	}
}
